import { FeatureManager } from '@amcax/base';
import i18n from '@app-base/common/utils/i18next';
import { getTableButtonList } from '@app-base/common/widgets/CircleBar/config';
import { CircleBarTableButtonItem } from '@app-base/common/widgets/CircleBar/types';

/**
 * 条件性获取按钮列表
 */
export const getConditionalButtons = <T>(
  condition: boolean,
  getArray: () => T[],
): T[] => {
  if (condition) {
    return [];
  }
  return getArray();
};

/**
 * 获取按钮列表，根据omitViewer条件过滤
 */
export const getButtonList = (
  omitViewer: boolean = true,
): CircleBarTableButtonItem[] => {
  return getTableButtonList(omitViewer);
};

/**
 * 按环境和功能过滤按钮列表
 */
export const filterButtonsByEnvironment = (
  buttons: CircleBarTableButtonItem[],
  environment: string,
): CircleBarTableButtonItem[] => {
  return buttons.filter(
    (button) =>
      button && button.environment && button.environment.includes(environment),
  );
};

/**
 * 按功能标志过滤按钮列表
 */
export const filterButtonsByFeatureFlag = (
  buttons: CircleBarTableButtonItem[],
): CircleBarTableButtonItem[] => {
  return buttons.filter(
    (button) =>
      !button.featureFlag ||
      FeatureManager.hasFeature(button.featureFlag) ||
      FeatureManager.isFeatureEnabled(button.featureFlag),
  );
};

/**
 * 按搜索文本过滤按钮列表
 */
export const filterButtonsBySearchText = (
  buttons: CircleBarTableButtonItem[],
  searchText: string,
): CircleBarTableButtonItem[] => {
  if (!searchText) {
    return buttons;
  }

  const searchLower = searchText.toLowerCase();
  return buttons.filter((button) => {
    const label = button.label || '';
    const type = button.type || '';
    return (
      label.toLowerCase().includes(searchLower) ||
      type.toLowerCase().includes(searchLower) ||
      i18n.t(label).toLowerCase().includes(searchLower) ||
      i18n.t(type).toLowerCase().includes(searchLower)
    );
  });
};
